package com.itheima.service.system.job;

import com.itheima.dao.system.UserDao;
import com.itheima.domain.system.User;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class BriJob {

    @Autowired
    private UserDao userDao;

    @Autowired
    private AmqpTemplate template;


    public void send() throws ParseException {
        List<User> user = userDao.findAllUser();
        //获得员工生日,比较
        for (User user1 : user) {
            String birthday = user1.getBirthday();
            String to = user1.getEmail();

            if (StringUtils.isNotEmpty(birthday)) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date bir = sdf.parse(birthday);
                Calendar c = Calendar.getInstance();
                c.setTime(bir);
                //获取今天日期
                Date date = new Date();
                Calendar d = Calendar.getInstance();
                d.setTime(date);
                //比较今天是否有员工生日
                if (c.get(Calendar.MONTH) == d.get(Calendar.MONTH) && c.get(Calendar.DATE) == d.get(Calendar.DATE)) {
                    //发邮件
                    String title = "生日快乐";
                    String content = "生日快乐";

                    //4. 向MQ中发送消息
                    Map map = new HashMap<>();
                    map.put("to", to);
                    map.put("title", title);
                    map.put("content", content);

                    template.convertAndSend("mail.send", map);
                    System.out.println("发送邮件了");
                }
            }
        }
    }
    public void test(){
        System.out.println("测试");
    }
}
